package com.cici.controller;


import com.cici.body.requestbody.TbTestStudentCreateRequestBody;
import com.cici.body.requestbody.TbTestStudentUpdateRequestBody;
import com.cici.entity.TbTestStudent;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cici.service.ITbTestStudentService;

import org.springframework.web.bind.annotation.RestController;



import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import com.cici.exception.ExceptionEnumImpl;
import com.cici.exception.common.RestNotFoundException;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;
import static org.springframework.http.HttpStatus.OK;



import javax.validation.Valid;

/**
 * <p>
 * TbTestStudent 前端控制器
 * </p>
 *
 * @author cici
 * @since 2018-11-30
 */
@RestController
@Slf4j
@RequestMapping(path ="tb_test_students")
public class TbTestStudentController {
    @Autowired
    private ITbTestStudentService  tbTestStudentService;


    @ApiOperation(value = "创建", notes = "创建", httpMethod = "POST")
    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<TbTestStudent> createTbTestStudent(@RequestBody TbTestStudentCreateRequestBody body) {
            return Optional.ofNullable(tbTestStudentService.create(body))
                    .map(s->new ResponseEntity<>(s,OK))
                    .orElseThrow(()->new RestNotFoundException(ExceptionEnumImpl.REQUEST_BODY_IS_NULL,
                    "Create TbTestStudent is fail ."));

            }


    @ApiOperation(value = "更新", notes = "更新", httpMethod = "PUT")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<TbTestStudent>  updateTbTestStudent(@PathVariable long id, @RequestBody TbTestStudentUpdateRequestBody body) {

            return Optional.ofNullable(tbTestStudentService.update(id, body))
                    .map(s -> new ResponseEntity<>(s, OK))
                    .orElseThrow(() -> new RestNotFoundException(ExceptionEnumImpl.REQUEST_BODY_IS_NULL,
                    "update TbTestStudent is fail."));
     }

    @ApiOperation(value = "获取detail", notes = "获取detail", httpMethod = "GET")
    @RequestMapping(value = "/{id}/get", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<TbTestStudent>  getTbTestStudent(@PathVariable("id") long id) {

        return Optional.ofNullable(tbTestStudentService.getTbTestStudent(id))
                .map(s -> new ResponseEntity<>(s, OK))
                .orElseThrow(() -> new RestNotFoundException(ExceptionEnumImpl.REQUEST_BODY_IS_NULL,
                        "update TbTestStudent is fail."));
    }


    @ApiOperation(value = "删除", notes = "删除", httpMethod = "DELETE")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity deleteById(@PathVariable long id) {
            return Optional.of(tbTestStudentService.deleteById(id))
                    .map(s -> new ResponseEntity<>(s, OK))
                    .orElseThrow(() -> new RestNotFoundException(ExceptionEnumImpl.REQUEST_BODY_IS_NULL,
                    "delete TbTestStudent is fail  ."));
    }






}
